package Frame;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Dao.DaoSqlData;
import Rule.LimitedDocument;
import Rule.NameTag;
import Rule.PatternCompile;

public class AlterInfoDialog extends JDialog{

	JButton button_Alter = null;
	JButton button_Exit = null;
	JTextField tfName = null;
	JTextField tfNo = null;
	JTextField tfPri = null;
	String sNum ;
	
	public AlterInfoDialog(String sName, String sNo, String sPri, String sNum){
		this.sNum = sNum;
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int width=screenSize.width/2-300;
		int height=screenSize.height/2-300;
		setBounds(width,height,180,190);
		
		setResizable(false);
		setModal(true);
		getContentPane().setLayout(new GridLayout(4,1,0,0));
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle(NameTag.DialogTittle_Name);
		
		JLabel lName = new JLabel(NameTag.common_Name);
		JLabel lNo = new JLabel(NameTag.common_No);
		JLabel lPri = new JLabel(NameTag.common_Pri);	
		
		LimitedDocument limitedDoc = new LimitedDocument(50);
		LimitedDocument limitedDoc_1 = new LimitedDocument(9);
		LimitedDocument limitedDoc_2 = new LimitedDocument(5);
		
		tfName = new JTextField(limitedDoc,sName,7);
		tfNo = new JTextField(limitedDoc_1,sNo,7);
		tfPri = new JTextField(limitedDoc_2,sPri,7);	
		button_Alter = new JButton(NameTag.table_buttonName);
		button_Alter.addActionListener(new ButtonListener());
		button_Exit = new JButton(NameTag.DialogFrame_Exit);
		button_Exit.addActionListener(new ButtonListener());
		
		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();
		JPanel panel3 = new JPanel();
		JPanel panel4 = new JPanel();
		panel1.add(lName);
		panel1.add(tfName);
		panel2.add(lNo);
		panel2.add(tfNo);
		panel3.add(lPri);
		panel3.add(tfPri);
		panel4.add(button_Alter);
		panel4.add(button_Exit);
			
		getContentPane().add(panel1);
		getContentPane().add(panel2);
		getContentPane().add(panel3);
		getContentPane().add(panel4);
		
		setVisible(true);
		
	}
	
	/**
	 * 按钮事件监听器
	 */
	private class ButtonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(e.getSource() == button_Exit){
				dispose();
			}
			
			else if (e.getSource() == button_Alter){
				if(tfName.getText().trim().equals("") || tfNo.getText().trim().equals("") || tfPri.getText().trim().equals("")){
					JOptionPane.showMessageDialog(null, "不能为空值");
					return;
				}
				if(! PatternCompile.isIntger(tfNo.getText().trim())){
					JOptionPane.showMessageDialog(null, "品号输入不符");
					tfNo.requestFocus();
					tfNo.selectAll();
					return;
				}			
				if(! PatternCompile.isDecimal(tfPri.getText().trim())){
					JOptionPane.showMessageDialog(null, "金额输入不符");
					tfPri.requestFocus();
					tfPri.selectAll();
					return;
				}
				boolean b = false;
				String sName = tfName.getText().trim();
				int iNo = Integer.valueOf(tfNo.getText().trim());
				float fPri = Float.parseFloat(tfPri.getText().trim());
				int iNum = Integer.valueOf(sNum);			
					
				try {
					b = DaoSqlData.alterPrdInfo(sName, iNo, fPri, iNum);
				} catch (Exception e1) {

					e1.printStackTrace();
				}
				ManagerUpdatePanel.Button_serch_2.doClick();
				dispose();
			}
		}	
	}

}
